import java.util.Arrays;

public class Solution910 {
    public int smallestRangeII(int[] nums, int k) {
        Arrays.sort(nums);
        int ans=nums[nums.length-1]-nums[0];
        for (int i=0;i<nums.length-1;i++){
            int max=Math.max(nums[nums.length-1]-k,nums[i]+k);
            int min=Math.min(nums[0]+k,nums[i+1]-k);
            ans=Math.min(ans,max-min);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution910().smallestRangeII(new int[]{1,3,6},3));
    }
}
